package com.dinghui.salary.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.dinghui.salary.pojo.Page;
import com.dinghui.salary.pojo.SalaryCalculateEquation;
import com.dinghui.salary.pojo.dto.SalaryCalculateEquationDTO;
import com.dinghui.salary.service.SalaryCalculateEquationService;
import com.dinghui.salary.utils.ApiResult;
import com.dinghui.salary.utils.GetUUID;
import com.dinghui.salary.utils.LocalUser;
import com.dinghui.salary.utils.PagedGridResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author tanrentao
 * @date 2021-09-06
 * @desc 用户登录
 */
@RestController
@RequestMapping("/SalaryCE")
@Api(value = "SalaryCalculateEquationController", tags = "工资计算公式")
public class SalaryCalculateEquationController {

    @Reference(version = "2.0.0")
    SalaryCalculateEquationService Service;

    @ApiOperation("增加或修改")
    @PostMapping("/add")
    public ApiResult add (@RequestBody SalaryCalculateEquationDTO dto) throws Exception{
        Date date = new Date();
//        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        SalaryCalculateEquation mouthType = new SalaryCalculateEquation();
        if(dto.getId() != null){
            mouthType.setId(dto.getId());
            mouthType.setUpdateBy(LocalUser.getUser().getId().toString());
            mouthType.setUpdateTime(date);
        }else{
            mouthType.setId(GetUUID.getUUID());
        }
        mouthType.setName(dto.getName());
        mouthType.setType(dto.getType());
        mouthType.setTime(dto.getYear());
        mouthType.setExpression(dto.getExpression());
        mouthType.setExpressionList(dto.getExpressionList());
        mouthType.setCreateBy(LocalUser.getUser().getId().toString());
        mouthType.setCreateTime(date);
        mouthType.setIsDelete(false);
        int i = 0;
        if(dto.getId() != null){
            i= Service.modify(mouthType);
        }else{
             i= Service.add(mouthType);
        }
        return ApiResult.success(i);
    }

    @ApiOperation("删除")
    @PostMapping("/Delete")
    public ApiResult Delete(@RequestBody SalaryCalculateEquationDTO dto) {
        SalaryCalculateEquation mouthType = new SalaryCalculateEquation();
        mouthType.setId(dto.getId());
        mouthType.setIsDelete(true);
        return ApiResult.success(Service.Delete(mouthType));
    }

    @ApiOperation("查询工资类型列表")
    @PostMapping("/FindAll")
    public ApiResult FindAll(@RequestBody Page dto) {
        PagedGridResult pagedGridResult = Service.FindAll(dto);
        return ApiResult.success(pagedGridResult);
    }

    @ApiOperation("根据Id查询")
    @PostMapping("/FindById")
    public ApiResult FindById(@RequestBody SalaryCalculateEquationDTO dto) {
        SalaryCalculateEquation mouthType = new SalaryCalculateEquation();
        mouthType.setId(dto.getId());
        return ApiResult.success(Service.FindById(mouthType));
    }

    @ApiOperation("查询all")
    @PostMapping("/getAllList")
    public ApiResult getAllList() {
        List<SalaryCalculateEquation> list = Service.getAllList();
        return ApiResult.success(list);
    }
}
